/*
 * c7_review13.cpp
 *
 *  Created on: Sep 1, 2013
 *      Author: cyper.yin
 */

#include <iostream>
using namespace std;
struct applicant {
	char name[30];
	int credit_ratings[3];
};

void f1(applicant *);
const char * f2(const applicant *, const applicant *);

int main_c7r13(int argc, char **argv) {
	applicant app = { "cyper", { 1, 2, 3 } };
	applicant * a = &app;
	applicant * a1 = &app;
	applicant * a2 = &app;

	//version 1, the intimidating way.
	cout << " ================== version 1 ==================" << endl;
	void (*pf1)(applicant *) = f1;
	pf1(a);

	const char * (*pf2)(const applicant *, const applicant *) = f2;
	pf2(a1, a2);

	void (*ap[5])(applicant *) = {f1,f1,f1,f1,f1};
	for (int i = 0; i < 5; i++) {
		ap[i](a);
	}

	const char *(*ap2[10])(const applicant *,
			const applicant *) = {f2,f2,f2,f2,f2,f2,f2,f2,f2,f2};

	const char *(*(*pa)[10])(const applicant *, const applicant *) = &ap2;
	for (int i = 0; i < 10; i++) {
		(*pa)[i](a1, a2);
	}

	//version 2, using typedef
	typedef void (*p_fun1)(applicant *);
	typedef const char * (*p_fun2)(const applicant *, const applicant *);

	cout << " ================== version 2 ==================" << endl;
	p_fun1 p = f1;
	p(a);

	p_fun1 funs1[5] = { f1, f1, f1, f1, f1 };
	for (int i = 0; i < 5; i++) {
		funs1[i](a);
	}

	p_fun2 p2 = f2;
	p2(a1, a2);

	p_fun2 funs2[10] = { f2, f2, f2, f2, f2, f2, f2, f2, f2, f2 };
	p_fun2 (*pa2)[10] = &funs2;
	for (int i = 0; i < 10; i++) {
		(*pa2)[i](a1, a2);
	}

	return 0;

}

void f1(applicant * a) {
	cout << "f1 is called" << endl;
}

const char * f2(const applicant * a1, const applicant * a2) {
	cout << "f2 is called" << endl;
	return "hello";
}

